﻿@model ASC.Models.ModuleModel
@using ASC.Resources;
@{
    ViewBag.Title = "Add";
}
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script>
 $(document).ready(function(){
      $("#Sphone").mask("9.99");

      jQuery(function($){
       $.mask.definitions['~']='[ 0123456789]';
       $("#Sphone").mask("99/99/9999",{placeholder:" "});
        });
    });

    $(document).ready(function () {
       

        SetTypeDescription();

    });

    function SetTypeDescription() {
        $('textarea[name="TypeDescription"]').val($('select[name="Module\\.ModuleType_Id"] option:selected').attr("description"));
    }



    var selectedTable= "";
    var currentValue = "";
    var currentTd = null;

    function SelectParametr(td){

        if(selectedTable != "")
        {
            if(selectedTable=="ModuleParameters")
            {
                UnSelectParametr(currentTd);
            }
            else
            {
                UnSelectChannel(currentTd);
            }
        }

        selectedTable = $(td).parent().parent().parent().attr("id");
        currentTd = td;

        $(td).parent().css("background-color","Cyan");
        $(td).removeAttr("onclick");
        currentValue = $(td).next().next().text();
        $(td).next().next().text("");
        $(td).next().next().append("<input id='ParamValue' type='text' value='" + currentValue + "' style='Width:70px; height: 12px' />" );
        $("#ParamValue").val(currentValue);
        $("#ParamValue").mask("~~~9.~~",{placeholder:" "}).val(currentValue);
    }

     function UnSelectParametr(currentTd){

        var td = currentTd;
        $(td).parent().css("background-color","White");

        $(td).attr("onclick", "SelectParametr(this)");
        if($("#ParamValue").val()=="")
            $(td).next().next().text("0.00");
        else
            $(td).next().next().text($("#ParamValue").val());

        $("#ParamValue").remove();
        td = null;
        selectedTable= "";
        currentValue = "";
        currentTd = null;
    }

     function SelectChannel(td){
        if(selectedTable != "")
        {
            if(selectedTable=="ModuleParameters")
            {
                UnSelectParametr(currentTd);
            }
            else
            {
                UnSelectChannel(currentTd);
            }
        }

        selectedTable = $(td).parent().parent().parent().attr("id");
        
        currentTd = td;
        
        $(td).parent().css("background-color","Cyan");
        $(td).removeAttr("onclick");
        
        currentValue = $(td).next().next().next().text();
        $(td).next().next().next().text("");

        $(td).next().next().next().append("<select id='ChannelsByChannelType' style='Width:100%; height: 16px' />" );
        currentValue =  $(td).attr("id");
        var channelTypeId = $(td).next().attr("id");

        $.ajax({
        
                  type: 'POST',
                  url: "@Url.Action("GetChannelsByChannelType", "Module")",
                  data: { channelTypeId: channelTypeId},
                  success: function(data){ 

                        $('#ChannelsByChannelType').append(
                             $('<option></option>').val(0).html('&lt;none&gt;')
                        );
                        $.each(data.ChannelsByChannelType, function(index, val) {
                            if (currentValue == val.Channel_Id )
                            {
                                $('#ChannelsByChannelType').append(
                                      $('<option selected></option>').val(val.Channel_Id).html(val.ChannelName)
                                );
                            }
                            else
                            {
                                $('#ChannelsByChannelType').append(
                                     $('<option></option>').val(val.Channel_Id).html(val.ChannelName)
                                );
                            }
                        });
                  },
                  dataType: 'json'
                 });

    }

    function UnSelectChannel(currentTd){
        var td = currentTd;
        $(td).parent().css("background-color","White");

        $(td).attr("onclick", "SelectChannel(this)");

        $(td).attr("id",$("#ChannelsByChannelType option:selected").val());
        $(td).next().next().next().text($("#ChannelsByChannelType option:selected").text());

        selectedTable= "";
        currentValue = "";
        currentTd = null;
    }

    function BeforePost()
    {
        if(selectedTable != "")
        {
            if(selectedTable=="ModuleParameters")
            {
                UnSelectParametr(currentTd);
            }
            else
            {
                UnSelectChannel(currentTd);
            }
        }

        CreateParamArrayOpbject();
        CreateChannelArrayOpbject();

        return true;
    }

    function CreateParamArrayOpbject()
    {
        var paramCount = 0;
        paramsArray = [];
        $("#ModuleParameters").find("tr").each(function(tr) {

        if($(this) != 'undefined')
        {
            if ($(this).attr("id")!='HeaderParameters')
            {
            peramObj={
                        Nr:     $(this).find("td:first-child").text() ,
                        Name:   $(this).children('td').eq(1).text(),
                        Value:  $(this).children('td').eq(2).text(),
                        Unit:   $(this).children('td').eq(3).text()
                        };
            
            paramsArray[paramCount] = peramObj;
            paramCount++;
            
            }
            
        }
           
        });
        paramCount = 0;

        var jsonText = JSON.stringify(paramsArray);
        $("#paramsJSON").val(jsonText);
    }

    function CreateChannelArrayOpbject()
    {
        var channelCount = 0;
        channelsArray = [];
        $("#ModuleChannels").find("tr").each(function(tr) {

        if($(this) != 'undefined')
        {
            if ($(this).attr("id")!='HeaderChannels')
            {
                if($(this).find("td:first-child").attr("id") != "0")
                {
                    channelObj={
                                Nr:     $(this).find("td:first-child").text() ,
                                Channel_Id:   $(this).find("td:first-child").attr("id")
                                };
            
                    channelsArray[channelCount] = channelObj;
                    channelCount++;
                }
            
            }
        }
           
        });
        if (channelCount > 0)
        {
            var jsonText = JSON.stringify(channelsArray);
            $("#channelsJSON").val(jsonText);
            channelCount = 0;
        }
        else
        {
            $("#channelsJSON").val("");
        }
    }

    function BeforeSaveAsNew(){
        if(selectedTable != "")
        {
            if(selectedTable=="ModuleParameters")
            {
                UnSelectParametr(currentTd);
            }
            else
            {
                UnSelectChannel(currentTd);
            }
        }

        CreateParamArrayOpbject();
        CreateChannelArrayOpbject();

        var newName = $.trim($("#Module_Name").val());

        $.ajax({
                  type: 'POST',
                  url: "@Url.Action("ValidateModuleName", "Module")",
                  data: { newName: newName},
                  success: function(data){ 

                        if(data.Exists == "1") {alert("'" + newName + "' " + "@Module.ModuleExists");  } else { $('#IsNew').val("1"); 

                        if ($("#FrmEdit").valid()) {
                            $.post($("#FrmEdit").attr("action"), $("#FrmEdit").serialize());
                        }
                     }
                  },
                  dataType: 'json'
        });
      
    }

    function SetChannelsEmpty(){
        if(selectedTable != "")
        {
            if(selectedTable!="ModuleParameters")
            {
                UnSelectChannel(currentTd);
            }
         }

        $("#ModuleChannels").find("tr").each(function(tr) {

        if($(this) != 'undefined')
        {
            if ($(this).attr("id")!='HeaderChannels')
            {
                $(this).children('td').eq(3).text("<none>");
                $(this).find("td:first-child").attr("id","0");
            }
        }
           
        });
    }

    
    function RedirectToChannelAdd()
    {
        window.open('@Url.Action("Add", "Channel")' + "?ModuleType_Id=" + $('select[name="Module\\.ModuleType_Id"] option:selected').val(),"channelAdd", 'width=710 , height=610, location=0,menubar=0,resizable=0, status=0,toolbar=0') ;
    }
  
</script>
@using (Html.BeginForm("Edit", "Module", FormMethod.Post, new { name = "FrmEdit", id = "FrmEdit" }))
{
        @Html.HiddenFor(x => x.Module.Module_Id)

<table style="width: 570px">
    <tr>
        <td class="centerAlign">
            <table class="maxWidth">
                <tr>
                    <td class="centerAlign">
                        <h2>@Global.FrmModuleEdit</h2>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table>
                <tr>
                    <td class="rightAlign" style="width: 70px">
                        @Module.Name:
                    </td>
                    <td class="leftAlign" style="width: 130px">
                        @Html.TextBoxFor(x => x.Module.Name, new { style = "width:150px" })
                        @Html.ValidationMessageFor(x => x.Module.Name)
                    </td>
                    <td class="rightAlign" style="width: 100px">
                        @Module.Used:
                    </td>
                    <td class="leftAlign">
                        @Html.CheckBoxFor(x => x.Module.Used)
                    </td>
                    <td class="rightAlign" style="width: 108px">
                        @Module.Type:
                    </td>
                    <td>
                        <select name="Module.ModuleType_Id" style="width: 140px" >
                                <option value="@Model.Module.ModuleType_Id" selected="selected" Description="@Model.ModuleTypes.FirstOrDefault(x => x.ModuleType_Id == Model.Module.ModuleType_Id).Description">@Model.ModuleTypes.FirstOrDefault(x => x.ModuleType_Id == Model.Module.ModuleType_Id).Name</option>
                        </select>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table>
                <tr>
                    <td class="rightAlign" style="width: 70px; vertical-align: text-top">
                        @Module.Description:
                    </td>
                    <td class="leftAlign">
                        @Html.TextAreaFor(x => x.Module.Description, new { style = "width:275px; height: 50px " })
                    </td>
                    <td class="rightAlign" style="width: 100px; vertical-align: text-top">
                        @Module.TypeDescription:
                    </td>
                    <td class="leftAlign">
                    
                        @Html.TextArea("TypeDescription", "", new { style = "width:245px; height: 50px", disabled = "disabled" })
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table cellpadding="3">
                <tr>
                    <td>
                        @Module.Parameters
                    </td>
                    <td colspan="2">
                        @Module.Chnnels
                        &nbsp;
                        <input type="button" name="CreateNewChannel" class="WideButton" value="@Module.CreateNewChannel" onclick="RedirectToChannelAdd()" />
                        &nbsp;
                        <input type="button" name="ClearChannels" class="WideButton" value="@Module.ClearChannels" onclick="SetChannelsEmpty()"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style="width: 350px; height: 150px; background-color: White; border: 1px solid #dddddd;
                            padding-left: 1px; overflow:scroll">
                            <input type="hidden" name="paramsJSON" id="paramsJSON"/>
                            <table class="maxWidth" id="ModuleParameters">
                                <thead style="background-color: #eee">
                                    <tr id="HeaderParameters" >
                                        <td style="width: 15px;">@Module.ParamNo
                                        </td>
                                        <td style="width: 75px">@Module.ParamName
                                        </td>
                                        <td style="width: 75px">@Module.ParamValue
                                        </td>
                                        <td style="width: 75px">@Module.ParamDevice
                                        </td>
                                    </tr>
                                </thead>
                                    @if (Model.Parameters != null)
                                    {
                                        foreach (var item in Model.Parameters)
                                        {
                                            <tr id="@item.ModuleTypeParameter_Id">
                                                <td onclick="SelectParametr(this)">@item.Nr</td><td>@item.Name</td><td>@string.Format("{0:f2}", item.Value)</td><td>@item.Unit</td>
                                            </tr>
                                        }
                                    }
                            </table>
                        </div>
                    </td>
                    <td>
                        <div style="width: 350px; height: 150px; background-color: White; border: 1px solid #dddddd;
                            padding-left: 1px; overflow:scroll">
                            <input type="hidden" name="channelsJSON" id="channelsJSON"/>
                            <table class="maxWidth" id="ModuleChannels">
                                <thead style="background-color: #eee">
                                    <tr id="HeaderChannels">
                                        <td style="width: 15px;">@Module.ChNo
                                        </td>
                                        <td style="width: 75px">@Module.ChName
                                        </td>
                                        <td style="width: 75px">@Module.ChType
                                        </td>
                                        <td style="width: 75px">@Module.ChChannel
                                        </td>
                                    </tr>
                                </thead>
                                 @if (Model.ModuleTypeChannels != null)
                                 {
                                     foreach (var item in Model.ModuleTypeChannels)
                                     {
                                            <tr id="@item.ModuleType_ChannelType_Id"> 
                                                @{
                                         ASC.DB.Module_Channel modulChannel = null;
                                                 }

                                                @if (Model.Module_Channels.Count > 0)
                                                {
                                                    modulChannel = Model.Module_Channels.FirstOrDefault(x => x.Nr == item.Nr);
                                                }
                                                    
                                                @if (modulChannel != null)
                                                {
                                                    <td onclick="SelectChannel(this)" id="@modulChannel.Channel_Id">@item.Nr</td>
                                                }
                                                else
                                                {
                                                    <td onclick="SelectChannel(this)" id="0">@item.Nr</td>
                                                }
                                              
                                                <td id="@item.ChannelType_Id">@item.Name</td>
                                                @{
                                                var channelType = Model.ChannelTypes.FirstOrDefault(x => x.ChannelType_Id == item.ChannelType_Id);
                                                }
                                                <td>
                                                     @channelType.Description
                                                </td>
                                                @if (modulChannel != null)
                                                {
                                                    <td> @Model.Channels.FirstOrDefault(x => x.Channel_Id == modulChannel.Channel_Id).Name</td>
                                                }
                                                else
                                                {
                                                    <td >&lt;none&gt;</td>
                                                }
                                          </tr>
                                     }
                                 }
                            </table>
                        </div>
                                              
                    </td>
                </tr>
               
            </table>
        </td>
    </tr>
    
    <tr>
        <td class="rightAlign">
            <input type="hidden" name="IsNew" id="IsNew" value="" />
            <table class="maxWidth">
                <tr>
                   <td style="width:100px"></td>
                    <td>
                        <input type="submit" name="Save" value="@Global.Save" class="StandartButton" onclick="return BeforePost()" />
                    </td>
                     <td>
                        <input type="button" name="SaveAsNew" value="@Global.SaveAsNew" class="StandartButton" onclick="BeforeSaveAsNew()" />
                    </td>
                     <td>
                        
                        <input type="submit" name="Action.Delete" value="@Global.Remove" class="StandartButton"  />
                    </td>
                     <td>
                        <input type="button" name="New" value="@Global.New" class="StandartButton" onclick="@Url.Action("Add", "Module")" />
                    </td>
                    <td>
                        <input type="button" name="Cancel" value="@Global.Cancel" onclick="window.location.href='/'" class="StandartButton" />
                    </td>
                    <td style="width:100px"></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
 
}
